package ru.st.selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class AddFilm extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testAddFilm() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
    driver.findElement(By.name("imdbid")).clear();
    driver.findElement(By.name("imdbid")).sendKeys("0000");
    driver.findElement(By.name("name")).clear();
    driver.findElement(By.name("name")).sendKeys("Keep Calm And Speak Esperanto");
    driver.findElement(By.name("aka")).clear();
    driver.findElement(By.name("aka")).sendKeys("Teni Trankviligi Kaj Paroli Esperanto");
    driver.findElement(By.name("year")).clear();
    driver.findElement(By.name("year")).sendKeys("2014");
    driver.findElement(By.name("duration")).clear();
    driver.findElement(By.name("duration")).sendKeys("90");
    driver.findElement(By.name("rating")).clear();
    driver.findElement(By.name("rating")).sendKeys("10");
    driver.findElement(By.name("trailer")).clear();
    driver.findElement(By.name("trailer")).sendKeys("http://www.youtube.com/watch?v=RlftmTm8I18");
    driver.findElement(By.name("notes")).clear();
    driver.findElement(By.name("notes")).sendKeys("The best film ever!");
    driver.findElement(By.name("taglines")).clear();
    driver.findElement(By.name("taglines")).sendKeys("Esperanto, Lingvo Internacia");
    driver.findElement(By.name("plotoutline")).clear();
    driver.findElement(By.name("plotoutline")).sendKeys("The film about Esperanto.");
    driver.findElement(By.name("plots")).clear();
    driver.findElement(By.name("plots")).sendKeys("The film is about Esperanto and the people who speak it.\nYou will speak Esperanto, too!");
    driver.findElement(By.id("text_languages_0")).clear();
    driver.findElement(By.id("text_languages_0")).sendKeys("Esperanto");
    driver.findElement(By.name("subtitles")).clear();
    driver.findElement(By.name("subtitles")).sendKeys("epo, eng, rus");
    driver.findElement(By.name("country")).clear();
    driver.findElement(By.name("country")).sendKeys("Russian Empire");
    driver.findElement(By.name("genres")).clear();
    driver.findElement(By.name("genres")).sendKeys("Action, Adventure, Biographies");
    driver.findElement(By.name("director")).clear();
    driver.findElement(By.name("director")).sendKeys("ratamara");
    driver.findElement(By.name("writer")).clear();
    driver.findElement(By.name("writer")).sendKeys("ratamara");
    driver.findElement(By.name("producer")).clear();
    driver.findElement(By.name("producer")).sendKeys("ratamara");
    driver.findElement(By.name("music")).clear();
    driver.findElement(By.name("music")).sendKeys("ratamara");
    driver.findElement(By.name("cast")).clear();
    driver.findElement(By.name("cast")).sendKeys("L. L. Zamenhof, me & you");
    driver.findElement(By.id("submit")).click();
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
